<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="/static/js/jquery.min.js"></script>
    <link href="/static/css/alert.css" rel="stylesheet">
    <link href="/static/css/css.css" rel="stylesheet">
    <style>
    </style>
</head>
<body>
    <div id="order-info" class="order-create-message">
        <table>
            <thead>
                <tr>
                    <!-- <th></th> -->
                    <th>域名</th>
                    <th>项目套餐</th>
                    <th>资料</th>
                    <th>要求备注</th>
                    <!-- <th>购买年限</th> -->
                    <th>付费后到期时间</th>
                    <!-- <th>完成时间</th> -->
                    <th>价格</th>
                    <th>
                        <a class="order-create-message-x" href="javascript:order_info_none();">X</a>
                    </th>
                </tr>
            </thead>
            <tbody id="order-message">
            </tbody>
            <tfoot>
            </tfoot>
        </table>
    </div>
    <div class="order-info-message" id="order-info-message">
        <div class="message-title">
            资料填写
        </div>
        <div class="message-data">
            <div class="message-input">
                <input type="text" id="nickname" placeholder="客户名称">
            </div>
            <div class="message-image">
                <img src="/static/images/upload.jpg" id="image-src" onclick="upload()">
                <input type="text" id="image-value" placeholder="营业执照" disabled>
            </div>
            <div class="message-button">
                <a href="javascript:set_order_message();">确认</a>
                <a href="javascript:none_order_message();">取消</a>
            </div>
        </div>
    </div>
    <div class="main">
        <div class="search-data" id="sreach_list">
            <div class="search-div">
                <label for="order_no">订单号:</label>
                <input type="text" id="order" placeholder="订单号" value="{$order}">
            </div>    
            <div class="search-div">
                <label for="nickname">客户名称:</label>
                <input type="text" id="nickname" placeholder="客户名称">
            </div>    
            <div class="search-div">
                <label for="domain_name">域名:</label>
                <input type="text" id="domain_name" placeholder="域名">
            </div>
            <div class="search-div">
                <label for="status">授权状态:</label>
                <input type="text" id="status" placeholder="授权状态">
            </div>
            <div class="search-div">
                <label for="open_time">投放时间:</label>
                <input type="text" id="open_time" placeholder="投放时间">
            </div>
            <div class="search-div">
                <label for="end_date">到期时间:</label>
                <input type="text" type="date" id="end_date" placeholder="到期时间">
            </div>
            <div class="search-div">
                <a href="javascript:;" onclick="order_search()">搜索</a>
                <a href="javascript:;" onclick="order_none()">重置</a>
                
                <!-- <a href="javascript:admin_order_create();">添加订单</a> -->

            </div>
        </div>
        <div class="order-page">
            <table class="order-page-table">
                <thead>
                    <tr>
                        <!-- <th>域名</th> -->
                        <th>订单</th>
                        <th>金额</th>
                        <th>下单账号</th>
                        <th>关键词</th>
                        <th>年限</th>
                        <th>下单时间</th>
                        <th>到期时间</th>
                        <th>项目状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="order-list">
                    {volist name="list" id="value"}
                    <tr>
                        <td>{$value.order_no}</td>
                        <td>{$value.amount}</td>
                        <td>{$value.username}</td>
                        <td>
                            {volist name="value.info" id="item"}
                            <span>{$item.keyword}</span>
                            {/volist}
                        </td>
                        <td>{$value.word_day}</td>
                        <td>{$value.create_time|date="Y-m-d"}</td>
                        <td>{$value.end_date}</td>
                        <td>{$value.status_text}</td>
                        <td>
                            <a href="/index/order/find?order_no={$value.order_no}" class="button">详情</a>   
                            <!-- <a href="javascript:order_info('{$value.order_no}');" class="button">续费</a>
                            <a href="javascript:order_info('{$value.order_no}');" class="button">支付</a> -->
                        </td>
                    </tr>
                    {/volist}
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="8">
                            <div class="page">
                                {$list|raw}
                            </div>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
        <div class="keywords-order-list-button" id="keywords-order-list-button" style="display:none;">
            <div class="total-price">
                <label for="total-price-1">总价:￥<b id="total-price"></b></label>
            </div>
        </div>
    </div>
    <input type="file" style="display: none;" id="file" accept="image/*">
</body>
<script src="/static/js/base.js"></script>
<script>
    var is_check = 1,check_count = 0,amount = 0,order_string = '';
    var order_no = "{$order_no}";
    order_init();
    function order_info_none(){
        block_mask_hide();
        document.getElementById('order-info').style ='display:none';
    }

    function order_info_show(){
        document.getElementById('order-info').style ='display:block';
    }

    function end_time(value,now){
        let tamp = Date.now();
        let time = Math.ceil(parseFloat(value) * 365) * 86400 * 1000;
        if(now)tamp = (new Date(now)).getTime();
        time = tamp + time;
        const date = new Date(time);
        const year = date.getFullYear();  
        const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以需要加1，并且用padStart确保是两位数  
        const day = String(date.getDate()).padStart(2, '0'); // 用padStart确保是两位数  
        document.getElementById('end_date').innerText = year + '-' + month + '-' + day;
        document.getElementById('order_amount').innerText = amount * parseFloat(value);
    }

    function order_init(){
        order_no = getQueryParam('order_no');
        // console.log(order_no);
        if (order_no){
            order_info(order_no);
        }
    }

    function order_info(order_no){
        let string = website = remover = option = '';
        $.ajax({
            url:'/index/order/info?order_no=' + order_no,
            type: 'post', // 请求类型，可以是 'GET' 或 'POST'  
            data:{},
            dataType: 'json', // 预期服务器返回的数据类型  
            success: function(res) {
                if (res.code == 200){
                    let info = res.data;
                    if(info != null){
                        block_mask_show();
                        amount = info.amount;
                        string += '<tr>';
                        if(info.status == 1){
                            if(info.website)
                                website = info.website;
                            string += '<td><input id="web" value="' + website + '"></td>';
                        }else{
                            if(info.website)
                                website = info.website;
                            string += '<td><input id="web" value="' + website + '" disabled></td>';
                        }
                        string += '<td>' + info.order_type_text + '</td>';
                        if (info.nickname && info.image)
                            remover = '<span class="message-a-yiwanshang">已完善</span>';
                        else
                            remover = '<span class="message-a-weiwanshang">未完善</span>';
                        console.log(remover);
                        string += '<td id="ziliao"><a href="javascript:show_order_message(\'' + info.order_no + '\');">' + remover + '</a></td>';
                        if (info.notes)
                            remover = info.notes;
                        else
                            remover = '';
                        string += '<td><input id="info" type="text" value="' + remover + '"></td>';
                        // for(let i=0.5;i<=5;i=i+0.5){
                        //     let m = Math.ceil(i * 12);
                        //     if(i == 1){
                        //     // if(info.buy_year == i){
                        //         option += '<option value="' + i + '" selected>' + m + '个月</option>';
                        //     }else{
                        //         option += '<option value="' + i + '">' + m + '个月</option>';
                        //     }
                        // }
                        // string += '<td><select id="buy_year" onchange="end_time(this.value)">' + option + '</select></td>';
                        string += '<td id="end_date">' + info.end_date + '</td>';
                        string += '<td id="order_amount">' + info.amount + '</td>';
                        if(info.status == 1)
                            string += '<td><a href="javascript:order_pay(\'' + order_no + '\');" class="button">支付</a></td>'
                        else
                            string += '<td><a href="javascript:order_pay(\'' + order_no + '\',2);" class="button">续费</a></td>';
                        string += '</tr>';
                        document.getElementById('order-info').style = 'display:block';
                        document.getElementById('order-message').innerHTML = string;
                        // end_time(1,info.end_date);
                        if(info.word_day == '全年')
                            end_time(1,info.end_date);
                        else{
                            end_time(0.5,info.end_date);
                        }
                    }
                }else{
                    show_after(res.msg,'获取数据失败');
                }
            }
        });
    }

    function getCurrentDate() {  
        const now = new Date();  
        const year = now.getFullYear(); // 获取完整的年份(4位数字)  
        let month = (now.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的，所以要加1，并使用padStart来确保是两位数  
        let day = now.getDate().toString().padStart(2, '0'); // 获取当前月份的日(1-31)，并确保是两位数  
        return {year:year,month:month,day:day};  
    }  

    function getQueryParam(paramName) {  
        let searchString = window.location.search.substring(1); // 去掉?号  
        let sParam = searchString.split('&');  
        for (let i = 0; i < sParam.length; i++) {  
            if (sParam[i].split('=')[0] === paramName) {  
                return sParam[i].split('=')[1];  
            }  
        }  
        return ;  
    } 

    function selectAll(){
        let ids = document.getElementsByName('ids');
        check_count = 0;
        for(index in ids){
            if (is_check == 1){
                check_count++;
                ids[index].checked = true;
            }else{
                ids[index].checked = false;
            }
        }
        if (is_check == 1) is_check = 0;
        else is_check = 1;
        selected_count();
    }

    function input_selected(id){
        let ids = document.getElementById('ids-' + id);
        // console.log(ids.checked);
        if (ids.checked == true){
            check_count++;
        }else{
            check_count--;
        }
        selected_count();
    }

    function selected_count(){
        document.getElementById('ids-count').innerText = check_count;
    }

    function order_search(){
        let item,string;
        let url = [];
        let sreach_list = document.getElementById('sreach_list');
        let input = sreach_list.getElementsByTagName('input');
        for(let i=0;i<input.length;i++){
            item = input[i];
            string = item.id + '=' + item.value;
            url.push(string);
        };
        url = '/index/order/list?' + url.join('&');
        console.log(url);
        location.href = url;
    }

    function show_order_message(order_no){
        order_string = order_no;
        let info = document.getElementById('order-info-message');
        info.style = 'display:block';
        $.ajax({
            url:"/index/order/info?order_no=" + order_no,
            type: 'post', // 请求类型，可以是 'GET' 或 'POST'  
            dataType: 'json', // 预期服务器返回的数据类型  
            success: function(res) { 
                if(res.code == 200){
                    let item = res.data;
                    document.getElementById('nickname').value = item.nickname;
                    if (item.image){
                        document.getElementById('image-src').src = item.image;
                        document.getElementById('image-value').value = item.image;
                    }else{
                        document.getElementById('image-src').src = '/static/images/upload.jpg';
                    }
                }
            }
        });
    }

    function none_order_message(){
        let info = document.getElementById('order-info-message');
        info.style = 'display:none';
    }

    function set_order_message(){
        let nickname = document.getElementById('nickname').value;
        let image = document.getElementById('image-value').value;
        $.ajax({
            url: '/index/order/save', // 你的 PHP 脚本的 URL  
            type: 'post', // 请求类型，可以是 'GET' 或 'POST'  
            dataType: 'json', // 预期服务器返回的数据类型  
            data: {
                order_no:order_string,
                nickname:nickname,
                image:image
            },  
            success: function(res) { 
                if(res.code == 200){
                    none_order_message();
                    if(res.data.order.nickname && res.data.order.image){
                        document.getElementById('ziliao').innerHTML = '<a href="javascript:show_order_message(\'' + res.data.order.order_no + '\');"><span class="message-a-yiwanshang">已完善</span></a>';
                    }
                }
            }
        });
    }

    const file = document.getElementById('file');  
    // 监听文件输入元素的 change 事件  
    file.addEventListener('change', function(event) {  
        // 获取选中的文件  
        const ff = event.target.files[0];  
        if (ff) {  
          // 读取文件为 Base64 编码  
          readFileAsBase64(ff);  
        } else {  
          console.error('没有选择文件');  
        }  
      });  

    function upload(){
        let file = document.getElementById('file');
        file.click();
    }

    function readFileAsBase64(file) {  
        return new Promise((resolve, reject) => {  
            const reader = new FileReader();  
            reader.onload = function(event) {  
                // 你可以在这里将 Base64 编码的数据发送到服务器或用于其他操作  
                const base64Image = event.target.result;  
                $.ajax({
                    url: '/image', // 你的 PHP 脚本的 URL  
                    type: 'post', // 请求类型，可以是 'GET' 或 'POST'  
                    dataType: 'json', // 预期服务器返回的数据类型  
                    data: {image:base64Image},  
                    success: function(res) { 
                       document.getElementById('image-src').src = res.data;
                       document.getElementById('image-value').value = res.data;
                    }
                });
            };  
            reader.onerror = function() {  
                reject('文件读取错误');  
            };  
            // 以 DataURL 的形式读取文件  
            reader.readAsDataURL(file);  
        });  
    }  

</script>
</html>